//


import UIKit

class WP_DailyViewController: WP_BasicViewController {
    
    //    var dataArray = Array<Any>()
    var isSSS:Bool = false
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        
        WP_CoreDataModel.coreDataManager.setEntityName(text: "DayModel")
        getData()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setNavTitle("Daily")
        // Do any additional setup after loading the view.
        setItem()
        self.view.addSubview(dayTableView)
        self.view.addSubview(empty)
    }
    
    
    lazy var dayTableView:UITableView = {
        
        let tableview = UITableView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight), style: .plain)
        tableview.delegate = self
        tableview.dataSource = self
        tableview.rowHeight = kRatioHeight(180)
        tableview.tableFooterView = UIView()
        tableview .register(WP_DayListTableViewCell.classForCoder(), forCellReuseIdentifier:NSStringFromClass(WP_DayListTableViewCell.classForCoder()))
        tableview.separatorColor = UIColor.white
        return tableview
        
    }()
    
    //    lazy var dataArray:NSMutableArray = {
    //        let array = NSMutableArray()
    //        return array
    //    }()
    lazy var dataArray:[Any] = {
        let array = Array<Any>()
        return array
    }()
    lazy var empty:WP_EmptyView = {
        let view = WP_EmptyView()
        view.isHidden = true
        view.delegate = self
        return view
    }()
    func setItem() {
        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
        button.setBackgroundImage(UIImage(named: "arrow39"), for: .normal)
        button.addTarget(self, action: #selector(itemClick), for: .touchUpInside)
        let leftV:UIBarButtonItem = UIBarButtonItem.init(customView: button)
        self.navigationItem.rightBarButtonItem = leftV
    }
    // 右键点击事件
    @objc func itemClick() {
        let vc = WP_DailyAddViewController()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    
    func getData()  {
        
        WP_CoreDataModel.coreDataManager.readData(sequenceKeys: ["remark","finish"], ascending: false, filterStr: nil, success: { (arr) in
            
            if arr.isEmpty && isSSS == false {
                //                let vc = WP_DailyAddViewController()
                //                self.navigationController?.pushViewController(vc, animated: true)
                //                isSSS = true
                //                return
                empty.isHidden = false
            }else{
                empty.isHidden = true
            }
            self.dataArray = arr
            dayTableView.reloadData()
            
        }, fail: {})
        
    }
    
}

extension WP_DailyViewController:UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataArray.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell:WP_DayListTableViewCell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(WP_DayListTableViewCell.classForCoder()), for: indexPath) as! WP_DayListTableViewCell
        //        cell.selectionStyle = .none
        
        cell.setModel(model: dataArray[indexPath.section] as! DayModel)
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = UIColorFromHex(0xFFFAF0)
        return view
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    
    //*
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool{
        return true
    }
    
    func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String?{
        return "删除"
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
    {
        let model = dataArray[indexPath.section] as! DayModel
        
        if editingStyle == .delete {
            print(model.iD!)
            WP_CoreDataModel.coreDataManager.deleteData(key: "iD = \(model.iD!)", success: {getData()}, fail: {})
        }
        
    }
    //*/
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let model = dataArray[indexPath.section] as! DayModel
        let vc = WP_DayDetailViewController()
        vc.model = model
        vc.ID = model.iD
        self.navigationController?.pushViewController(vc, animated: true)
        
    }
    
}

//empty代理
extension WP_DailyViewController:WP_EmptyViewDelegate{
    func GoBlcok() {
        let vc = WP_DailyAddViewController()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    
}
